<script>
import { ElMessage } from 'element-plus'
export default {
  name: "LoginView",
  data() {
    return{
      form: {username:'',password:''}
    }
  },
  methods:{
    login(){
      // 通过axios发送登录请求
      this.$axios.post('/login',this.$qs.stringify(this.form))
          .then(result => {
            console.log('login',result.data)
            if (result.data.code === 200){
              // 判断用户是否存在
              if (result.data.data) {
                ElMessage.success('登录成功')
                this.$router.push({path:'/Index'})
              } else {
                ElMessage.error('用户名或密码错误')
              }
            }
          })

    }
  }
}
</script>

<template>
  <el-card style="max-width: 480px;margin: 200px auto">
    <h1>欢迎使用学生管理系统</h1>
    <el-form :model="form" label-width="auto" style="max-width: 480px">
      <el-form-item label="账号">
        <el-input v-model="form.username" />
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.password" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="login" style="margin-left: 200px">登录</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<style scoped>

</style>